# -*- coding: utf-8 -*-

from web.controllers.api import route_api
from flask import request, jsonify
from application import app, db
import requests, json
from common.models.member.Member import Member
from common.models.device.CheckList import CheckList
from common.models.device.DeviceRelation import DeviceRelation
from common.libs.Helper import getCurrentDate


@route_api.route("/administrator/detail", methods=["GET", "POST"])
def admin_detail():
    resp = {'error': 200, 'msg': '操作成功', 'data': {}}
    content = request.get_json()
    usercode = content['usercode']
    check_id = content['homeid']

    member_info = Member.query.filter_by(usercode=usercode).first()

    if not member_info:
        resp['error'] = -1
        resp['msg'] = "无此用户"
        return jsonify(resp)

    member_id = member_info.id

    checklist_info = CheckList.query.filter_by(id=check_id).first()
    device_info_array = DeviceRelation.query.filter_by(check_id=check_id).all()
    data = dict()
    device_info_list = []

    data['sjdzt'] = checklist_info.administor_status
    data['sjdw'] = checklist_info.company
    data['wtrq'] = checklist_info.commission_date
    data['lxr'] = checklist_info.contact
    data['dh'] = checklist_info.mobile

    for device_item in device_info_array:
        device_info_list_item = dict()
        device_info_list_item['sbmc'] = device_item.device_name
        device_info_list_item['xhgg'] = device_item.model
        device_info_list_item['zzc'] = device_item.manufacture
        device_info_list_item['dj'] = device_item.rank
        device_info_list_item['ccbh'] = device_item.number
        device_info_list_item['yxrq'] = device_item.date
        device_info_list_item['jcjg'] = device_item.resault
        device_info_list_item['csry'] = device_item.testman_name
        device_info_list_item['sbid'] = device_item.device_id
        device_info_list_item['bhgyy'] = device_item.reason_back
        device_info_list_item['dhyy'] = device_item.reason_force
        device_info_list.append(device_info_list_item)

    data['deviceInfo'] = device_info_list

    resp['data'] = data
    return jsonify(resp)


@route_api.route("/administrator/commit", methods=["GET", "POST"])
def admin_commit():
    resp = {'error': 200, 'msg': '操作成功', 'data': {}}
    content = request.get_json()
    usercode = content['usercode']
    check_id = content['homeid']
    deviceList = content['deviceInfo']      # 这是一个list ,里面的元素是dict

    member_info = Member.query.filter_by(usercode=usercode).first()

    if not member_info:
        resp['error'] = -1
        resp['msg'] = "无此用户"
        return jsonify(resp)

    counter = 0

    for deviceList_item in deviceList:
        device_id = deviceList_item['sbid']
        testman_name = deviceList_item['csry']
        reason_force = deviceList_item['dhyy']

        device_info = DeviceRelation.query.filter_by(device_id=device_id, check_id=check_id).first()

        if reason_force.strip() == "":
            counter += 1
            device_info.test_status = "待测试"
        else:
            device_info.test_status = "打回不分配测试"

        device_info.testman_name = testman_name
        device_info.reason_force = reason_force
        device_info.update_time = getCurrentDate()
        db.session.commit()

    checklist_info = CheckList.query.filter_by(id=check_id).first()

    if counter == 0:
        checklist_info.member_status = "被打回"
        checklist_info.administor_status = "打回"
    else:
        checklist_info.member_status = "测试中"
        checklist_info.administor_status = "已分配测试"
    db.session.commit()

    return jsonify(resp)


@route_api.route("/administrator/forceback", methods=["GET", "POST"])
def forceback():
    resp = {'error': 200, 'msg': '操作成功', 'data': {}}
    content = request.get_json()

    check_id = content['homeid']
    forceback_reason = content['dhyy']
    usercode = content['usercode']

    member_info = Member.query.filter_by(usercode=usercode).first()

    if not member_info:
        resp['error'] = -1
        resp['msg'] = "无此用户"
        return jsonify(resp)

    checklist_info = CheckList.query.filter_by(id=check_id).first()

    checklist_info.member_status = "被打回"
    checklist_info.administor_status = "已反馈至客户"
    checklist_info.reason = forceback_reason
    checklist_info.update_time = getCurrentDate()
    db.session.commit()

    deviceinfo_list = DeviceRelation.query.filter_by(check_id=check_id).all()

    for deviceinfo_item in deviceinfo_list:
        deviceinfo_item.test_status = "已完成"
        deviceinfo_item.update_time = getCurrentDate()
        db.session.commit()

    return jsonify(resp)


@route_api.route("/administrator/feedback", methods=["GET", "POST"])
def feedback():
    resp = {'error': 200, 'msg': '操作成功', 'data': {}}
    content = request.get_json()
    usercode = content['usercode']
    check_id = content['homeid']

    member_info = Member.query.filter_by(usercode=usercode).first()

    if not member_info:
        resp['error'] = -1
        resp['msg'] = "无此用户"
        return jsonify(resp)

    checklist_info = CheckList.query.filter_by(id=check_id).first()

    checklist_info.member_status = "已完成"
    checklist_info.administor_status = "已反馈至客户"

    db.session.commit()

    return jsonify(resp)


